#!/bin/sh -e

ME=`dirname $0`
ME=`cd $ME; pwd`

[ -z "$MAKE_DMGS"    ] && MAKE_DMGS=1
[ -z "$DO_CLEAN"     ] && DO_CLEAN=0
[ -z "$DO_UPDATE"    ] && DO_UPDATE=1
[ -z "$DO_UNIVERSAL" ] && DO_UNIVERSAL=1
[ -z "$USE_SOURCES"  ] && USE_SOURCES=0

# phonon:trunk/kdereview kdebindings:trunk/KDE/kdebindings
PACKAGES="kdesupport:trunk/kdesupport kdelibs:trunk/KDE/kdelibs kdepimlibs:trunk/KDE/kdepimlibs kdebase:trunk/KDE/kdebase kdebase-runtime:trunk/KDE/kdebase kdeadmin:trunk/KDE/kdeadmin kdeartwork:trunk/KDE/kdeartwork kdeedu:trunk/KDE/kdeedu kdegames:trunk/KDE/kdegames kdegraphics:trunk/KDE/kdegraphics kdemultimedia:trunk/KDE/kdemultimedia kdenetwork:trunk/KDE/kdenetwork kdepim:trunk/KDE/kdepim kdesdk:trunk/KDE/kdesdk kdetoys:trunk/KDE/kdetoys kdeutils:trunk/KDE/kdeutils koffice:trunk/koffice kdevplatform:trunk/KDE/kdevplatform kdevelop:trunk/KDE/kdevelop kdewebdev:trunk/KDE/kdewebdev amarok:trunk/extragear/multimedia ktorrent:trunk/extragear/network/ktorrent"

DO_PACKAGES="$PACKAGES"
SVNROOT=https://svn.kde.org/home/kde
# for anonymous users:
# SVNROOT=svn://anonsvn.kde.org/home/kde

CMAKE_ARGS="-DBUNDLE_INSTALL_DIR=/opt/kde4/bin -DBUILD_k3b:BOOL=OFF -DBUILD_kaudiocreator=OFF -DBUILD_kmid=OFF -DBUILD_kplayer=OFF -DBUILD_kmplayer=OFF -DKDE4_DISABLE_MULTIMEDIA=ON -DXMMS_FOUND:BOOL=OFF -DENABLE_REGENERATEXSD:BOOL=OFF -DWITH_GNOKII:BOOL=OFF -DWITH_FAM:BOOL=OFF -DBUILD_PHONON_QT7:BOOL=OFF -DKDE_DISTRIBUTION_TEXT='Mac OS X Installer Packages (http://mac.kde.org/)'"

case `uname -s` in
	Linux)
		MAKE_DMGS=0
		;;
esac

do_help() {
	cat <<END;
usage: $0 [-h] [-c] [-n] [-r] [-s]

	-h|--help          this help

	-c|--clean         do a clean build (erase compiledir before building)
	-f|--first-package start building at the specified package
	-n|--no-package    don't erase kde-mac/* and create new packages
	-r|--revision      check out a specific revision
	-s|--skip-update   don't do an 'svn up'

END
}

ARG_PACKAGES=""
last_arg=""
for ARG in "$@"; do
	case $ARG in
		-c|--c|--cl|--cle|--clea|--clean)
			DO_CLEAN=1
			;;
		-f|--f|--fi|--fir|--firs|--first|--first-|--first-p|--first-pa|--first-pac|--first-pack|--first-packa|--first-packag|--first-package)
			last_arg="first_package"
			;;
		-n|--n|--no|--no-|--no-p|--no-pa|--no-pac|--no-pack|--no-packa|--no-packag|--no-package)
			MAKE_DMGS=0
			;;
		-s|--s|--sk|--ski|--skip|--skip-|--skip-u|--skip-up|--skip-upd|--skip-upda|--skip-updat|--skip-update)
			DO_UPDATE=0
			;;
		-r|--r|--re|--rev|--revi|--revis|--revisi|--revisio|--revision)
			last_arg="revision"
			;;
		-v|--sv|--svn|--svn-|--svn-r|--svn-ro|--svn-roo|--svn-root)
			last_arg="svnroot"
			;;
		-h|--h|--he|--hel|--help)
			do_help
			exit 0
			;;
		*)
			case $last_arg in
				revision)
					UP_REVISION="$ARG"
					last_arg=""
					;;
				first_package)
					START_AT="$ARG"
					last_arg=""
					;;
				svnroot)
					SVNROOT="$ARG"
					last_arg=""
					;;
				*)
					ARG_PACKAGES="$ARG_PACKAGES $ARG"
					;;
			esac
			;;
	esac
done

if [ -n "$ARG_PACKAGES" ]; then
	DO_PACKAGES=""
	for ARG in $ARG_PACKAGES; do
		for dir in $PACKAGES; do
			PACKAGE=`echo $dir | cut -d: -f1`
			if [ "$PACKAGE" = "$ARG" ]; then
				DO_PACKAGES="$DO_PACKAGES $dir"
			fi
		done
	done
fi

. "$ME/environment-helper.sh"

SVNROOT=`echo $SVNROOT | sed -e 's,/$,,'`
KDEDIR="/opt/kde4"

[ "$DO_CLEAN" -eq 1 ] && rm -rf "$KDEDIR"

get_sourcefile() {
	package="$1"; shift
	ls $ME/sources/$PACKAGE-*.tar.bz2 2>/dev/null | sort -u | head -n 1
}

get_sourcefiledir() {
	package="$1"; shift
	sourcefile=`get_sourcefile "$1"`
	echo $sourcefile | sed -e 's,.tar.bz2$,,'
}

if [ "$DO_UPDATE" -eq 1 ]; then
	for dir in $DO_PACKAGES; do
		PACKAGE=`echo $dir | cut -d: -f1`
		SVNPATH=`echo $dir | cut -d: -f2`
		SOURCEFILE=`get_sourcefile $PACKAGE`
		SOURCEFILEDIR=`get_sourcefiledir $PACKAGE`

		if [ "$USE_SOURCES" = 1 ] && [ -n "$SOURCEFILE" ] && [ -e "$SOURCEFILE" ]; then

			if [ ! -d "../source.build/$PACKAGE/.svn" ] && [ "../source.build/$PACKAGE/kdemac-source" -nt "$SOURCEFILE" ]; then
				echo "$SOURCEFILE has already been unpacked"
			else
				echo "unpacking $SOURCEFILE"
				pushd sources
					tar -xjf "$SOURCEFILE"
					rsync -ar --delete "$SOURCEFILEDIR/" "../source.build/$PACKAGE/"
					rm -rf "$SOURCEFILEDIR/"
					if [ -e "$ME/sources/$PACKAGE.patch" ]; then
						pushd "../source.build/$PACKAGE"
							patch -p1 --fuzz=4 < $ME/sources/$PACKAGE.patch
						popd
					fi
					touch "../source.build/$PACKAGE/kdemac-source"
				popd
			fi

		else

			if [ "$PACKAGE" = "kdebase-runtime" ]; then
				continue
			fi

			if [ -n "$UP_REVISION" ]; then
				SVN_REV_ARGS="-r $UP_REVISION"
			fi

			echo "updating $PACKAGE"
			if [ ! -e "$ME/source.build/$PACKAGE" ] || [ ! -e "$ME/source.build/$PACKAGE/.svn" ]; then
				rm -rf "$ME/source.build/$PACKAGE" "$ME/compile.build/$PACKAGE"
				if [ `echo $SVNPATH | grep -c -E '^/'` -gt 0 ]; then
					svn co $SVNREV_ARGS "${SVNROOT}${SVNPATH}" "$ME/source.build/$PACKAGE"
				else
					svn co $SVNREV_ARGS "${SVNROOT}/$SVNPATH" "$ME/source.build/$PACKAGE"
				fi
			else
				pushd "$ME/source.build/$PACKAGE"
					svn up $SVN_REV_ARGS
				popd
			fi

			if [ "$PACKAGE" = "kdesupport" ]; then
				# special case, use phonon branch
				pushd source.build/kdesupport/phonon
					svn switch $SVNREV_ARGS "${SVNROOT}/branches/phonon/4.2"
				popd
			fi

		fi
	done
fi

STARTED=0
for dir in $DO_PACKAGES; do
	PACKAGE=`echo $dir | cut -d: -f1`
	SVNPATH=`echo $dir | cut -d: -f2`

	if [ "$USE_SOURCES" = 0 ] && [ "$PACKAGE" = "kdebase-runtime" ]; then continue; fi

	if [ -n "$START_AT" ] && [ "$STARTED" = "0" ]; then
		if [ "$PACKAGE" = "$START_AT" ]; then
			STARTED=1
		else
			continue
		fi
	fi

	if [ "$PACKAGE" = "phonon" ] && [ ! -d "/opt/qt4/share/qt-private-headers" ]; then
		# phonon won't build without qt-copy around
		continue
	fi

	[ "$DO_CLEAN" -eq 1 ] && rm -rf "$ME/compile.build/$PACKAGE"
	mkdir -p "$ME/compile.build/$PACKAGE"

	pushd "$ME/compile.build/$PACKAGE"
		/usr/bin/make -j1 uninstall >/dev/null 2>&1 || :
		DO_CMAKE=0
		if [ ! -e "$ME/compile.build/$PACKAGE/CMakeCache.txt" ] || [ ! -e "$ME/compile.build/$PACKAGE/Makefile" ]; then
			DO_CMAKE=1
		fi
		if [ -e "$ME/compile.build/$PACKAGE/Makefile" ] && [ `find $ME/source.build/$PACKAGE -type f -newer $ME/compile.build/$PACKAGE/Makefile | wc -l` -gt 0 ]; then
			DO_CMAKE=1
		fi
		if [ "$USE_SOURCES" = 1 ] && [ "$ME/source.build/$PACKAGE/kdemac-source" -nt "$ME/compile.build/$PACKAGE/CMakeCache.txt" ]; then
			DO_CMAKE=1
		fi
		if [ "$DO_CMAKE" = 1 ]; then
			cmake -DCMAKE_INSTALL_PREFIX="$KDEDIR" $CMAKE_ARGS "$ME/source.build/$PACKAGE"
			find . -name link.txt | xargs $ME/clean-linking.pl
			find . -name flags.make | xargs perl -pi -e 's,-F/System/Library/Frameworks,,g'
			if [ "$PACKAGE" = "phonon" ] && [ -d "/opt/qt4/share/qt-private-headers" ]; then
				perl -pi -e "s,/opt/qt4/include,/opt/qt4/share/qt-private-headers,g" CMakeCache.txt
			fi
		fi
		/usr/bin/make
		if [ "$MAKE_DMGS" = 1 ]; then
			rm -rf "$ME/$PACKAGE/root/"*
			/usr/bin/make -j1 install DESTDIR="$ME/$PACKAGE/root"
			pushd ../..
				rm -rf kde-mac/$PACKAGE-* kde-mac/$PACKAGE.*
				/usr/bin/make -j1 kde-mac/$PACKAGE.torrent
			popd
			rm -rf "$ME/$PACKAGE/root/"*
		fi
		/usr/bin/make -j1 install
	popd
done
